﻿

//*************** INITIALISATION **********************

$(document).ready(
    function () {
        gameInit()
});

function gameInit() {

    //Initilise the canvas *************
    setZoom(1, _Zoom);
    _Canvas = document.getElementById("cnvPlayArea");
    _Context = _Canvas.getContext("2d");

    _Canvas.width = _CanvasWidth;
    _Canvas.height = _CanvasHeight;
    //**********************************


    //Initilise the virual map************
    _CurrentMap = new mapClass(_CanvasWidth, _CanvasHeight);
    _AllParticles = new Array();

    //Randomly add items to the map
    var velocityDecimalPlacesMultiplier = Math.pow(10, _VelocityDecimalPlaces);
    for (var i = 0; i <= _InitialeParticleCount; i++) {

        var newObject = new particleClass();
        _AllParticles[i] = newObject;

        var rndX = Math.floor(Math.random() * _CanvasWidth);
        var rndY = Math.floor(Math.random() * _CanvasHeight);
        var rndVelocityX = (Math.floor(Math.random() * _MaxVelocity * 2 * velocityDecimalPlacesMultiplier) - (_MaxVelocity * velocityDecimalPlacesMultiplier)) / velocityDecimalPlacesMultiplier;
        var rndVelocityY = (Math.floor(Math.random() * _MaxVelocity * 2 * velocityDecimalPlacesMultiplier) - (_MaxVelocity * velocityDecimalPlacesMultiplier)) / velocityDecimalPlacesMultiplier;

        newObject._velocityX = rndVelocityX;
        newObject._velocityY = rndVelocityY;

		if (i < 5)
		{
			newObject._mass = 2 * _DefaultMass;
			newObject._color = "blue";
		}else{
	        newObject._mass = _DefaultMass;
		}

        //Makes sure that two particles are not added to the same location
        while(_CurrentMap.hasParticle(rndX,rndY))
        {
            rndX = Math.floor(Math.random() * _CanvasWidth);
            rndY = Math.floor(Math.random() * _CanvasHeight);
        }

        _CurrentMap.setParticle(rndX, rndY, newObject);

    }
    //**********************************

    //****** Start the timer ********
    StartTimer();
    //*******************************

}

function setZoom(oldZoom, newZoom) {
    _CanvasWidth = (_CanvasWidth / oldZoom) * newZoom;
    _CanvasHeight = (_CanvasHeight / oldZoom) * newZoom;
    _ParticleSize = (_ParticleSize / oldZoom) * newZoom;
}

//********************* UPDATING ***************************

function gameTick() {

    moveParticles();
    updatePlayArea();

	outputTotalSystemVelocity();

}


//*********Drawing*************
function updatePlayArea() {

    clearPlayArea();
    for (var i = 0; i < _CurrentMap.innerParticles.length(); i++) {
        var particleParts = _CurrentMap.innerParticles.get(i);
        var posX = particleParts[0];
        var posY = particleParts[1];
        var particle = particleParts[2];
        drawOnPlayArea(posX, posY, particle);
    }

}

function clearPlayArea() {

    // Store the current transformation matrix
    _Context.save();

    // Use the identity matrix while clearing the canvas
    _Context.setTransform(1, 0, 0, 1, 0, 0);
    _Context.clearRect(0, 0, _Canvas.width, _Canvas.height);

    // Restore the transform
    _Context.restore();

}

function drawOnPlayArea(posX, posY, particle) {

    //red square
    _Context.fillStyle = particle.color();
    _Context.fillRect(posX, posY, _ParticleSize, _ParticleSize);

}

//*********Moving****************
function moveParticles() {

    _NewMap = new mapClass(_CanvasWidth, _CanvasHeight);

    for (var i = 0; i < _CurrentMap.innerParticles.length(); i++) {
        var particleParts = _CurrentMap.innerParticles.get(i);
        var posX = particleParts[0];
        var posY = particleParts[1];
        var particle = particleParts[2];

        moveParticle(posX, posY, particle);

    }
	processCollisions(_NewMap);

    _CurrentMap.dispose();
    _CurrentMap = _NewMap;

}

function moveParticle(CurrentPosX, CurrentPosY, particle){

    _NewMap.addParticle(CurrentPosX + particle._velocityX,
                        CurrentPosY + particle._velocityY,
                        particle);

}

function processCollisions(map) {
	
	var collisions = map.innerCollidedParticles.all();
	for (key in collisions) {
		if (collisions.hasOwnProperty(key))
		{
			var pos = collisions[key];
	        Interaction_ElasticCollision(map.getParticleMulti(pos[0],pos[1]));
		}
    }

}




//************************** Debug **************************************
function outputTotalSystemVelocity(){

	if(_DebugMode ) {
		
		var totalX = 0;
		var totalY = 0;
	
	    for (var i = 0; i < _CurrentMap.innerParticles.length(); i++) {
	    	
	        var particle = _CurrentMap.innerParticles.get(i)[2];
	    	
	    	totalX += Math.abs(particle._velocityX * particle._mass * 2);
	    	totalY += Math.abs(particle._velocityY * particle._mass * 2);
	    	
	    }
	    
	    DG(totalX + ", " + totalY)
   	}

}
function DG(msg) {
	if(_DebugMode ) {
		$("#pnlDebug").html(msg);
		window.focus();
	}
}